Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-traverse-tree

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-traverse-tree

Asynchronously iterates and transforms tree-like structures.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

async-traverse-tree

npm version Build Status Coverage Status License

async-traverse-tree is a lightweight, asynchronous library for traversing and mapping tree-like data structures.

Features

  • Asynchronously traverses and modifies objects and arrays.
  • Easy-to-use API for applying custom functions to each value in the tree.
  • Prevents circular references to maintain data integrity.

Installation

You can install async-traverse-tree using npm or yarn:

npm install async-traverse-tree

or

yarn add async-traverse-tree

Usage

Here's a simple example of how to use async-traverse-tree with the updated implementation:

// Import the 'traverse' function from 'async-traverse-tree'
import { traverse } from 'async-traverse-tree';

// Define a custom mapper function
const mapper = async (key, value) => {
  // Apply your custom logic here asynchronously
  return value;
};

// Your data structure
const data = /* Your data structure here */;

// Asynchronously traverse and map the data
traverse(data, mapper)
  .then(result => {
    // Process the result
    console.log(result);
  })
  .catch(error => {
    console.error(error);
  });

Controlling Tree Traversal

The async-traverse-tree module exports two symbols that can be used to control the traversal process:

  • STOP: Returning STOP from your mapper function will halt traversal of the current branch. No child nodes will be processed.
  • REMOVE: Returning REMOVE from your mapper function will remove the current key/value pair from the tree.
import { traverse, STOP, REMOVE } from 'async-traverse-tree';

const mapper = async (key, value) => {

  // stop traversing deeper into this branch
  if (skipCondition) {
    return STOP
  }

  // remove this key/value completely
    if (removeCondition) {
    return REMOVE
  }

  // Apply your custom logic here asynchronously
  return value;
};

Motivation

An easy way to iterate over an object-like structure is to use JSON.parse(JSON.stringify(obj), (key, value) => ... ).
The problem with this approach is that it can't handle circular references and is synchronous. async-traverse-tree can act like an async drop-in replacement for that which can also handle circular references.

Keywords

FAQs

Package last updated on 16 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc